import { WatcherTyp, VmTyp, WatchCallback } from './types'
import { getValue } from './utils'
import Dep from './Deps'
export default class Watcher implements WatcherTyp{
    key:string
    vm: VmTyp
    oldVal: any
    callback: WatchCallback
    constructor(vm: VmTyp, key: string, callback: WatchCallback) {
        this.callback = callback
        this.key = key
        this.vm = vm
        Dep.target = this
        this.oldVal = getValue(vm, key)
        Dep.target = null
        this.callback = callback
    }
    update(){
        let nVal = getValue(this.vm, this.key)
        this.callback && this.callback(nVal, this.oldVal)
        this.oldVal = nVal
    }
}